<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 30px;
	padding-top: 9px;
	}
	
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
 	.mytable td{
   	padding-top: 5px;
   	}
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">
//集中获取ModelAndView数据
var path = "${pageContext.request.contextPath}" + "/";
var grid = null; // 生成表格
var searchPanel = null;// 生成panel
var searchFiled = null;// 生成搜索字段
var serchButton = null; // 生成搜索按钮
var mainTool = null; // 生成工具栏
var dialogAdd = null; // 生成明细对话框
var addForm = null;
var divInputChooser =  null;// 生成输入选择框
var validator = null;
//设置表格操作提交的网址
var searchUrl = path + "rm/filterQuery.html";
var inputReceiveUrl = path+"rm/MatReceiveCtl/inputReceive.html";
$(function() {
	initVars();
	loadLayout();// 加载布局
	loadGrid();// 加载表格
	loadSearchPanel();// 加载搜索面板
	loadButtonBar();// 加载工具栏
	loadInputHelperButton();
	loadDialogAdd();
	validateInit();
	search();// 搜索
});

function initVars(){
	//集中定义jQuery选择器
	grid = $("#maingrid"); // 生成表格
	searchPanel = $("#search-panel");// 生成panel
	searchFiled = $("#search-field");// 生成搜索字段
	serchButton = $('#search-button'); // 生成搜索按钮
	mainTool = $('#maintool'); // 生成工具栏
	dialogAdd = $("#dialog-Add"); // 生成明细对话框
	addForm = $("#addForm");
	inputHelper = $('#inputHelper');
	divInputChooser =  $("#divInputChooser");// 生成输入选择框
}

function loadLayout(){
	$('#page').omBorderLayout({
		panels : [ {
			id : "center-panel",
			header : false,
			region : "center"
		} ]
	});
	$('#search-panel').omPanel({
		title : "搜索",
		collapsible : true,
		height : 155
	});
}

function loadGrid(){
	grid.omGrid({
		limit : 30,
		singleSelect : true,
		title : '原料进厂数据',
		height:500,
		colModel : [
				{header : '计量单号', name : 'measureId', align : 'center', width : '130', sort:'serverSide'},
				{header : '进厂类型', name : 'deliveryType', align : 'center', width : '80', sort:'serverSide'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '90', sort:'serverSide'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '220', sort:'serverSide'},
				{header : '净重(KG)', name : 'netWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '毛重(KG)', name : 'grossWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '皮重(KG)', name : 'tareWeight', align : 'center', width : '100', sort:'serverSide'},
				{header : '时间戳', name : 'timeStamp', align : 'center', width : '160', sort:'serverSide'},
		            ]
	});
}

function loadSearchPanel() {
	$('#search-field input').each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				OnBtnQuery();
			}
		});
	});
	$('#queryDeliveryType').omCombo({
		dataSource : [ {
			text : '直供',
			value : '直供'
		}, {
			text : '配送',
			value : '配送'
		} , {
			text : '调拨',
			value : '调拨'
		} ],
		width : 163
	});
	$('#deliveryType').omCombo({
		dataSource : [ {
			text : '直供',
			value : '直供'
		}, {
			text : '配送',
			value : '配送'
		} , {
			text : '调拨',
			value : '调拨'
		} ],
		editable : false,
		width : 163,
		value : '直供'
	});
	$("#receiveTime").omCalendar({showTime : true});
	$("#queryTimeStampS").omCalendar({showTime : true});
	$("#queryTimeStampE").omCalendar({showTime : true});
	$("#receiveTime").css("width","140px");
	$("#queryTimeStampS").css("width","140px");
	$("#queryTimeStampE").css("width","140px");
	
	serchButton.omButton({
		icons : {
			left : path + 'resources/sm/images/search.png'
		},
		width : 70,
		onClick : function(event) {
			search();
		}
	});
}

function loadButtonBar(){
	var buttons = [];
	var btn = {
			id:"operationInput",
			label:"录入",
			onClick:function(){
				showDialogAdd("收货录入");
			}
	};
	buttons.push(btn);
	buttons.push({ separtor : true	});
	mainTool.omButtonbar({
		btns:buttons
	});
}

function search(){
	var rules =[], i=0;
	$(':input','#searchTable').each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	rules[i++] = {op : 'startwith', field : 'measureId', value : 'MR', type : 'string'};
	var group ={
			op : "and",
			rules : rules
	};
	var extraData = {
			"domain" : "SmRmMeasureDoc",
			"rules" : i==0?null:JSON.stringify(group),
	};
	grid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
}
function getSubmitData(form)
{
	var info = {};
	$(':input',form).each(function(){
		if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
			info[$(this).attr('id')] = $(this).val();
		}
	});

	return JSON.stringify(info);
}

function matReceive(){
	
	var info = {
			"SmRmMeasureDoc" : getSubmitData(dialogAdd)
	};
	$.post( inputReceiveUrl,info, 
			function(data, textStatus)
			{
		     if( data.success )
				{
		    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
		    	 grid.omGrid( "reload" );
				}
		     else
		    	 {
					$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
		    	 }						
			});
	dialogAdd.omDialog("close");
}

function loadDialogAdd(){
	var buttons = [ {
		text : "确定",
		click : function() {
			if(validator.form()){
				matReceive();
			}else{
				$(':input',addForm).each(function(){
					if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
						$(this).omTooltip('show');
					}
				});
			}
		}
	}, {
		text : "取消",
		click : function() {
			$(':input',addForm).each(function(){
				if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
					$(this).omTooltip('hide');
				}
			});
			dialogAdd.omDialog("close");
			grid.omGrid('reload');
			
		}
	} ];

	dialogAdd.omDialog({
		width : 800,
		height : 270,
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : buttons,
		onClose : function(){
			$(':input',addForm).each(function(){
				if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
					$(this).omTooltip('hide');
				}
			});
		}
	});
	// 显示dialog并初始化里面的输入框的数据
	showDialogAdd = function(title) {
		validator.resetForm();
		dialogAdd.omDialog("option", "title", title);
		dialogAdd.omDialog("open");// 显示dialog
	};
}
function loadInputHelperButton(){
	
	var selectUrl = path + "sm/select-input.html?jspName=sm/rm/matChooser";
	var frameId = "AnaInput";
	var buttons = [ {
		text : "关闭",
		click : function() {
			divInputChooser.omDialog("close");// 关闭dialog
		}
	} ];
	divInputChooser.omDialog({
		width : 600,
		height : 470,
		autoOpen : false,
		modal : true,
		resizable : false,
		targetmode : "iframe",
		name : frameId,
		url : selectUrl,
		buttons : buttons,
	});

	$('#materialId','#addForm').keydown(function(e){
        if(e.keyCode==118){ //如果按的键是F7
        	inputHelper.omButton('click');
            return false;
        }else{
            return false; //禁用输入其它内容
        }
    });
	
	inputHelper.omButton({
		width : 35,
		id : "inputHelperBtn",
		onClick : function(event){
			divInputChooser.omDialog("option", "title", "选择（双击记录即可选择）");
			divInputChooser.omDialog("open");// 显示dialog
			var childWindow = document.getElementById(frameId).contentWindow;
			childWindow.search();
        }
	});
	
}

function fillBackAndCloseDialog(rowData) {
	$("#materialId" , $("#addForm")).val(rowData["materialId"]);
	$("#materialDesc", $("#addForm")).val(rowData["materialDesc"]);
	divInputChooser.omDialog("close");
}

function validateInit()
{
	validator = $("#addForm").validate(
			{
				//编写验证规则
				rules: {
					materialId: { required: true },
					materialDesc: { required: true },
					grossWeight: { required: false, digits: true },
					tareWeight: { required: false, digits: true },
					netWeight: { required: true, digits: true },
					receiveTime :  {required: true },
					},
				messages: {
					materialId: { required: "" },
					materialDesc: { required: "" },
					netWeight: { required: "" },
					receiveTime: { required: "" },
					},
			});
	
	$('#materialId').omTooltip({
		  region : 'right',
       html : '不能为空！',
       anchor : true ,
       offset : [-5,45]
	});
	$('#materialDesc').omTooltip({
		  region : 'right',
     html : '不能为空！',
     anchor : true ,
     offset : [-5,5]
	});
	$('#grossWeight').omTooltip({region : 'right', html : '必须为数字！', anchor : true , offset : [-5,5]});
	$('#tareWeight').omTooltip({region : 'right', html : '必须为数字！', anchor : true , offset : [-5,5]});
	$('#netWeight').omTooltip({region : 'right', html : '非空，必须为数字！', anchor : true , offset : [-5,5]});
	$('#receiveTime').omTooltip({region : 'right', html : '不能为空！', anchor : true , offset : [-5,25]});
	
}

</script>

</head>
<body id="mainBody">	
	<div id="page" style="height:690px;">
		<div id="center-panel">
			<div id="search-panel"> 
				<div id="search-field">
				<table id="searchTable" class="mytable">
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryDeliveryType" name="deliveryType" op="equal" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">计量单号:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryMeasureId" name="measureId" op="startwith" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryMaterialId" name="materialId" op="startwith" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料描述:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryMaterialDesc" name="materialDesc" op="like" ty="string" type="text" /></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">时间戳从:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryTimeStampS" name="timeStamp" op="greaterorequal" ty="date" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">时间戳到:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryTimeStampE" name="timeStamp" op="lessorequal" ty="date" type="text" /></span></td>
					</tr>
				</table>
				</div>
				<div class="search-button"><span id="search-button">搜索</span></div>
			</div>
	    	<div id="maintool"></div>
			<table id="maingrid"></table>
		</div>    	
	</div>	
    <div id="dialog-Add">	
		<form id="addForm">
		<table  class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="materialId" name="materialId" type="text" /></span></td>
				<td><button id='inputHelper' value="浏览"></button></td>
			</tr>
		</table>
		<table  class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料描述:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="materialDesc" name="materialDesc"  type="text" readonly="readonly"/></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货时间:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="receiveTime" name="receiveTime" type="text" /></span></td>
			</tr>
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="deliveryType" name="deliveryType" type="text" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">毛重(KG):</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="grossWeight" name="grossWeight"  type="text" /></span></td>
			</tr>
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">皮重(KG):</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="tareWeight" name="tareWeight"type="text" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">净重(KG):</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="netWeight" name="netWeight" type="text" /></span></td>
			</tr>
		</table>
		</form>	
	</div>
	<div id = "divInputChooser"></div>
</body>
</html>